Imports System.Xml.Serialization
Imports Aricie.DNN.ComponentModel
Imports DotNetNuke.UI.WebControls
Imports System.ComponentModel
Imports Aricie.DNN.UI.Attributes
Imports Aricie.Collections
Imports Aricie.ComponentModel
Imports Aricie.DNN.Entities
Imports Aricie.DNN.Services.Filtering
Imports Aricie.DNN.UI.WebControls.EditControls
Imports Aricie.DNN.UI.WebControls

Namespace Aricie.DNN.Services.Url.FriendlyUrl

    <Serializable()> _
    Public Class FriendlierUrlConfig
        Inherits AutoModuleConfig(Of FriendlierUrlConfig)


#Region "Private members"

        Private _DefaultUrlProviders As New List(Of UrlProviderInfo)

        Private _ModuleUrlProviders As New SerializableDictionary(Of String, UrlProviderInfo)

        'Private _ReplaceRegexs As New SerializableDictionary(Of String, String)



        Private _FreezeUrlStore As Boolean
        Private _UrlStoreMaxSize As Integer = 1000000
        Private _UrlStoreFolder As String = "Aricie.FriendlyUrlStore"
        'Private _DiscardPortalAlias As Boolean

        Private _DiscardPortalAlias As Boolean

        <NonSerialized()> _
        Private _AvailableProviders As SerializableDictionary(Of String, UrlProviderInfo)

        Private _LogCachedObjectCreation As Boolean

        Private _OnDemandLookup As Boolean
        ' Private _OnDemandLookup As Boolean
        Private _PessimisticLookup As Boolean = True
        Private _UpperInvariantInStore As Boolean = True
        Private _SynchronousPersistence As Boolean
        'Private _FreezeUrlStore As Boolean
        Private _NoAutomaticOverwrite As Boolean
        Private _UseReverseLookup As Boolean
        Private _Turn404Into410 As Boolean

        'log
        Private _LogLimitConditions As Boolean
        ' Private _LogCachedObjectCreation As Boolean
        Private _LogMissingMandatory As Boolean
        Private _LogNotFoundAndNewMappings As Boolean
        Private _LogRequestTiming As Boolean
        Private _LogAnEventPerUrl As Boolean
#End Region

#Region "Public Properties"

        <ExtendedCategory("GeneralProviders"), _
       Editor(GetType(ListEditControl), GetType(EditControl)), _
     InnerEditor(GetType(PropertyEditorEditControl)), LabelMode(LabelMode.Top), IsReadOnly(False)> _
     <TrialLimited(DNN.Security.Trial.TrialPropertyMode.NoAdd Or DNN.Security.Trial.TrialPropertyMode.NoDelete)> _
        Public Property DefaultUrlProviders() As List(Of UrlProviderInfo)
            Get
                Return _DefaultUrlProviders
            End Get
            Set(ByVal value As List(Of UrlProviderInfo))
                _DefaultUrlProviders = value
            End Set
        End Property

        <ExtendedCategory("ModuleSpecificProviders")> _
            <Editor(GetType(DictionaryEditControl), GetType(EditControl))> _
            <KeyEditor(GetType(SelectorEditControl), GetType(ModuleUrlRewritersKeyAttributes))> _
            <ValueEditor(GetType(PropertyEditorEditControl))> _
            <LabelMode(LabelMode.Top), Orientation(System.Web.UI.WebControls.Orientation.Vertical)> _
            <CollectionEditor(False, True, True, True, 5, CollectionDisplayStyle.Accordion, True)> _
     <TrialLimited(DNN.Security.Trial.TrialPropertyMode.NoAdd Or DNN.Security.Trial.TrialPropertyMode.NoDelete)> _
        Public Property ModuleUrlProviders() As SerializableDictionary(Of String, UrlProviderInfo)
            Get
                Return _ModuleUrlProviders
            End Get
            Set(ByVal value As SerializableDictionary(Of String, UrlProviderInfo))
                _ModuleUrlProviders = value
            End Set
        End Property

        <ExtendedCategory("TechnicalSettings")> _
        <MainCategory()> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Property UrlStoreMaxSize() As Integer
            Get
                Return Me._UrlStoreMaxSize
            End Get
            Set(ByVal value As Integer)
                Me._UrlStoreMaxSize = value
            End Set
        End Property

        <ExtendedCategory("TechnicalSettings")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property DiscardPortalAlias() As Boolean
            Get
                Return _DiscardPortalAlias
            End Get
            Set(ByVal value As Boolean)
                _DiscardPortalAlias = value
            End Set
        End Property

        <Browsable(False), XmlIgnore()> _
        Public ReadOnly Property AvailableUrlrewriters() As SerializableDictionary(Of String, UrlProviderInfo)
            Get
                If _AvailableProviders Is Nothing Then
                    _AvailableProviders = New SerializableDictionary(Of String, UrlProviderInfo)
                    For Each rewriter As UrlProviderInfo In Me.DefaultUrlProviders
                        If rewriter.ProviderIsActive AndAlso rewriter.Rewrites.Count > 0 Then
                            _AvailableProviders(rewriter.Name) = rewriter
                        End If
                    Next
                    For Each rewriter As UrlProviderInfo In Me.ModuleUrlProviders.Values
                        If rewriter.ProviderIsActive AndAlso rewriter.Rewrites.Count > 0 Then
                            _AvailableProviders(rewriter.Name) = rewriter
                        End If
                    Next
                End If

                Return _AvailableProviders
            End Get
        End Property



        ''' <summary>
        ''' JBB : modification pour bug du portail par defaut
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <ExtendedCategory("TechnicalSettings")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property OnDemandLookup() As Boolean
            Get
                Return _OnDemandLookup
            End Get
            Set(ByVal value As Boolean)
                _OnDemandLookup = value
            End Set
        End Property

        <ExtendedCategory("TechnicalSettings")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property PessimisticLookup() As Boolean
            Get
                Return _PessimisticLookup
            End Get
            Set(ByVal value As Boolean)
                _PessimisticLookup = value
            End Set
        End Property

        <ExtendedCategory("TechnicalSettings")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property UpperInvariantInStore() As Boolean
            Get
                Return _UpperInvariantInStore
            End Get
            Set(ByVal value As Boolean)
                _UpperInvariantInStore = value
            End Set
        End Property

        <ExtendedCategory("TechnicalSettings")> _
        Public Property RealUrlComparison() As new ComparerInfo() With{.Comparison = StringComparison.OrdinalIgnoreCase} 

        <ExtendedCategory("TechnicalSettings")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property UseReverseLookup() As Boolean
            Get
                Return _UseReverseLookup
            End Get
            Set(ByVal value As Boolean)
                _UseReverseLookup = value
            End Set
        End Property

        <ExtendedCategory("TechnicalSettings")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property SynchronousPersistence() As Boolean
            Get
                Return _SynchronousPersistence
            End Get
            Set(ByVal value As Boolean)
                _SynchronousPersistence = value
            End Set
        End Property

        <ExtendedCategory("TechnicalSettings")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property FreezeUrlStore() As Boolean
            Get
                Return _FreezeUrlStore
            End Get
            Set(ByVal value As Boolean)
                _FreezeUrlStore = value
            End Set
        End Property

        <ExtendedCategory("TechnicalSettings")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Property NoAutomaticOverwrite() As Boolean
            Get
                Return Me._NoAutomaticOverwrite
            End Get
            Set(ByVal value As Boolean)
                Me._NoAutomaticOverwrite = value
            End Set
        End Property

        <ExtendedCategory("TechnicalSettings")> _
        <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property Turn404Into410() As Boolean
            Get
                Return _Turn404Into410
            End Get
            Set(ByVal value As Boolean)
                _Turn404Into410 = value
            End Set
        End Property


        ''' <summary>
        ''' JBB : modification pour bug du portail par defaut
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <ExtendedCategory("Debug")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property LogCachedObjectCreation() As Boolean
            Get
                Return _LogCachedObjectCreation
            End Get
            Set(ByVal value As Boolean)
                _LogCachedObjectCreation = value
            End Set
        End Property

        <ExtendedCategory("Debug")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property LogLimitConditions() As Boolean
            Get
                Return _LogLimitConditions
            End Get
            Set(ByVal value As Boolean)
                _LogLimitConditions = value
            End Set
        End Property

        <ExtendedCategory("Debug")> _
        Public Property LogMonitoredStrings As New EnabledFeature(Of List(Of String))



        <ExtendedCategory("Debug")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property LogMissingMandatory() As Boolean
            Get

                Return _LogMissingMandatory
            End Get
            Set(ByVal value As Boolean)
                _LogMissingMandatory = value
            End Set
        End Property

        <ExtendedCategory("Debug")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property LogNotFoundAndNewMappings() As Boolean
            Get

                Return _LogNotFoundAndNewMappings
            End Get
            Set(ByVal value As Boolean)
                _LogNotFoundAndNewMappings = value
            End Set
        End Property

        <ExtendedCategory("PerformanceMonitoring")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property LogRequestTiming() As Boolean
            Get

                Return _LogRequestTiming
            End Get
            Set(ByVal value As Boolean)
                _LogRequestTiming = value
            End Set
        End Property

        <ExtendedCategory("PerformanceMonitoring")> _
                <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property LogAnEventPerUrl() As Boolean
            Get

                Return _LogAnEventPerUrl
            End Get
            Set(ByVal value As Boolean)
                _LogAnEventPerUrl = value
            End Set
        End Property


        Private Class ModuleUrlRewritersKeyAttributes
            Implements IAttributesProvider


            Public Function GetAttributes() As System.Collections.Generic.IEnumerable(Of System.Attribute) Implements DNN.UI.Attributes.IAttributesProvider.GetAttributes
                Dim toReturn As New List(Of Attribute)
                toReturn.Add(New SelectorAttribute(GetType(DesktopModuleSelector), "FriendlyName", "ModuleName", True, False, "", "", False, False))
                toReturn.Add(New WidthAttribute(500))
                Return toReturn
            End Function
        End Class

#End Region

        Public Overrides Function GetModuleName() As String
            Return FriendlierUrlContext.MODULE_NAME
        End Function
    End Class
End Namespace